﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views;
using CMS.Presenters.Presenters;
using CMS.Data;
using ProjectBase.Utils;
using System.Collections.Specialized;

namespace CMS.Web.Template
{
    public partial class GopY : ViewUserControl, IGopYView
    {
        private GopYPresenter presenter;

        public GopY()
        {
            this.presenter = new GopYPresenter(this, NHibernateDaoFactory.Instance.GetGopYDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnOK.Attributes.Add("onclick", "return CheckValid();");
            
        }        

        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            this.tblAddNew.Visible = false;
            this.tblContactInfo.Visible = true;
            //send mail
            SendMail(this.GopYToAdd);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.FullBaseUrl);
            //btnCancel.Attributes.Add("onclick", "return backHome(\"" + this.FullBaseUrl + "\")");
        }

        #region IGopYView Members

        public string Message
        {
            set 
            {
                //lblMessage.Text = value; 
            }
        }

        public CMS.Core.Domain.GopY GopYToAdd
        {
            get 
            {
                CMS.Core.Domain.GopY obj = new CMS.Core.Domain.GopY();
                obj.HoTen = this.txtHoTen.Text.Trim();
                obj.DiaChi = this.txtDiaChi.Text.Trim();
                obj.DienThoai = this.txtDienThoai.Text.Trim();
                obj.Email = this.txtEmail.Text.Trim();
                obj.TieuDe = this.txtTieuDe.Text.Trim();
                obj.NoiDung = this.txtNoiDung.Text.Trim();
                obj.CMND = this.txtCMND.Text.Trim();//CMS.Web.Files.Instance.StandardName("gopy", this.txtFileUpload.PostedFile);

                return obj;
            }
        }

        #endregion

        private void SendMail(CMS.Core.Domain.GopY entity)
        {
            try
            {
                StringDictionary emailParams = new StringDictionary();

                emailParams["{name}"] = entity.HoTen;
                emailParams["{cmnd}"] = entity.CMND;
                emailParams["{add}"] = entity.DiaChi;
                emailParams["{content}"] = entity.NoiDung;

                string body = CMail.Instance.CreateEmailFromTemplate("~", "contact.txt", ref emailParams);
                CMail.Instance.SendMailInGmail(entity.Email, entity.TieuDe, body);
            }
            catch (Exception ex)
            {
                //String ext = ex.Message;
            }
        }
    }
}